package rjgc.mall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import rjgc.mall.entity.Goods;
import rjgc.mall.entity.Seller;
import rjgc.mall.entity.TradeAction;
import rjgc.mall.entity.User;
import org.w3c.dom.ls.LSException;
import rjgc.mall.entity.*;
import rjgc.mall.mapper.GoodsMapper;
import rjgc.mall.mapper.SellerMapper;
import rjgc.mall.mapper.TradeActionMapper;
import rjgc.mall.mapper.UserMapper;
import rjgc.mall.service.SellerService;
import rjgc.mall.service.exception.goods.GoodsOnlyOneException;
import rjgc.mall.service.exception.seller.PasswordNotMatchException;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerMapper sellerMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private TradeActionMapper tradeActionMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void publishGoods(String goodsName,
                             String goodsImgUrl,
                             String goodsDescription,
                             Float goodsPrice,
                             String goodsType,
                             Integer status,
                             Integer supply_id) {
        List<Goods> goods = goodsMapper.selectGoods(supply_id);
        if(goods.size() == 1 && !goods.isEmpty()){
            throw new GoodsOnlyOneException("只能上架一个商品");
        }
        sellerMapper.publishGoods(goodsName,
                goodsImgUrl,
                goodsDescription,
                goodsPrice,
                goodsType,
                status,
                supply_id, new Date()) ;
    }

    @Override
    public Seller Sellerlogin(String account) {
        Seller seller=sellerMapper.Sellerlogin(account);
        return seller;
    }

    @Override
    public void Passwordupdate(String account, String password,String oldPassword) {
        if(sellerMapper.checkPassword(account, oldPassword) == 0){
            System.out.println(sellerMapper.checkPassword(account, oldPassword));
            throw new PasswordNotMatchException("密码不正确");
        }
        sellerMapper.Passwordupdate(account, password);
    }


    @Override
    public User checkUser(Integer user_id) {
        User user=sellerMapper.checkUser(user_id);
        return user;
    }
    @Override
    public void Register(String account,String password,String name){
       sellerMapper.Register(account,password,name);
    }
    @Override
    public List<TradeAction> intendingUser(Integer goods_id)
    {
        return tradeActionMapper.intendingUser(goods_id);
    }

    @Override
    public User getintendingUser(Integer id) {
        return  sellerMapper.getintendingUser(id);
    }

    @Override
    public void chooseUser(Integer id,Integer goods_id) {
        sellerMapper.chooseUser(id);
        goodsMapper.chooseUser(goods_id);
    }

    @Override
    public boolean registerComfirm() {
        int x=sellerMapper.registerComfirm();
        System.out.println(x);
        if(x==0){
            return true;
        }
        else {
            return false;
        }

    }

    @Override
    public List<String> Buyerinfo(Integer id) {
        List<String> infoList= sellerMapper.Buyerinfo(id);
        return infoList;
    }

    @Override
    public Integer checkgoods() {
        return  goodsMapper.checkgood();
    }

    @Override
    public Integer getTradecount(Integer goodsId) {
        return  tradeActionMapper.getTradecount(goodsId);
    }

    @Override
    public Integer inquireStock(Integer goodsid){
        Integer account=goodsMapper.inquireStock(goodsid);
        return account;
    }

    @Override
    public void reviseStock(Integer goodsid) {
        goodsMapper.reviseStock(goodsid);
    }

    @Override
    public List<User> checkUser() {
        List<User> userList=userMapper.checkUser();
        return userList;
    }
    @Override
    public List<TradeAction> seeUsertradition(Integer user_id){
       List<TradeAction> tradeActionList=tradeActionMapper.seeUsertradition(user_id);
       return tradeActionList;
    }

    @Override
    public TradeAction seeTraditionbyid(Integer id) {
        TradeAction td=tradeActionMapper.seeTraditionbyid(id);
        return td;
    }

    @Override
    public List<Goods> seehistoricalgoods() {
        List<Goods> goodsList=goodsMapper.seehistoricalgoods();
        return goodsList;
    }

    @Override
    public List<Goods> goodsnotbuy() {
        List<Goods> goodsList=goodsMapper.goodsnotbuy();
        return goodsList;
    }

    @Override
    public User checkUserbyid(Integer id) {
        User u=userMapper.checkUserbyid(id);
        return u;
    }

    @Override
    public Goods checkgoodbyid(Integer id) {
        Goods g=goodsMapper.checkgoodbyid(id);
        return g;
    }

    @Override
    public void publishGoods2(String goodsName, List<String> ImgUrl, String goodsDescription, Float goodsPrice, String goodsType, Integer status, Integer supply_id,Integer stock,String select) {
        Goods goods = new Goods(goodsName, ImgUrl.get(0), goodsDescription, goodsPrice, goodsType, status, supply_id, new Date(), stock,select);
        sellerMapper.publishGoods2(goods) ;
        String ids = "";
        for(int i=0;i<ImgUrl.size()-1;i++){
            Img img = new Img();
            img.setImg(ImgUrl.get(i+1));
            goodsMapper.setImg(img);
            System.out.println(img.getId());
            ids= ids+img.getId() +" ";
        }
        goodsMapper.setImgId(ids,goods.getId());
    }

    @Override
    public void Automaticdismounting(Integer id) {
        goodsMapper.Automaticdismounting(id);

    }
    @Override
    public void DownGoods(String name){
        goodsMapper.DownGoods(name);

    }
    @Override
    public void CancelTrad(Integer id){
        tradeActionMapper.CancelTrad(id);
    }
    @Override
    public void AddStock(Integer id){
        goodsMapper.AddStock(id);
    }
    @Override
    public void UpGoods(Integer id){
        goodsMapper.UpGoods(id);
    }

    @Override
    public void completeGoods(Integer id) {
        tradeActionMapper.completeGoods(id);
    }


    //此后为本学期新增
    //result为2 ——> 订单未处理，查看订单页面按钮为“确认订单”，点击表明商家已确认订单，将result变更为4
    //result为4 ——> 订单已确认，查看订单页面显示为“待发货”，点击表明商家发货，将result变更为5，查看订单界面应显示“待收货”
    //之后交由买家进行交易完成操作，将result从5改为1
    /************将订单状态从确认订单变更为待发货**********/
    @Override
    public Integer prepareTheGoods(Integer tradeid) {
        if (tradeActionMapper.getTradeResult(tradeid)==2){
            return tradeActionMapper.prepareTheGoods(tradeid);
        }else{
            return 0;
        }
    }

    /************将订单状态从待发货变更为已发货**********/

    @Override
    public Integer prepareFineWatingforSendOut(Integer tradeid) {
        if (tradeActionMapper.getTradeResult(tradeid)==4){
            return tradeActionMapper.prepareFineWatingforSendOut(tradeid);
        }else{
            return 0;
        }
    }

    @Override
    public void AddStock2(Integer id, int num) {
        goodsMapper.AddStock2(id,num);
    }
}
